import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';

///**
///* @fileName: base_request_form_appointment_time
///* @author: hrongyong
///* @date-time: 11/4/21 2:25 PM
///* @email: hrongyong@gmail.com
///* description:
///* <fileName> <author> <date-time> <email> <description>
///* 文件名 作者姓名 创建时间 作者邮箱 文件描述
///*/
class BaseRequestFormAppointmentTime extends StatefulWidget {
  final BaseHttpRequestModel request;
  final RequestSaveData requestSaveData;

  const BaseRequestFormAppointmentTime({Key key, this.request, this.requestSaveData}) : super(key: key);

  @override
  _BaseRequestFormAppointmentTimeState createState() => _BaseRequestFormAppointmentTimeState();
}

class _BaseRequestFormAppointmentTimeState extends State<BaseRequestFormAppointmentTime> {
  ///控制器
  BaseRequestFormProvider get _baseRequestFormProvider => BaseRequestFormProvider.of(context, listen: false);

  Map<String, dynamic> _appointment = {};

  @override
  Widget build(BuildContext context) {
    _appointment = _baseRequestFormProvider.getDataSource(widget.request);
    return BaseRequestFormContainer(
      request: widget.request,
      onPress: () => _openSelectAppointmentTime(_appointment ?? {}),
      suffixIcon: BaseDropDownIcon(),
      showSuffixIcon: !_baseRequestFormProvider.isReadonly(widget.request),
      child: _buildChild(_appointment ?? {}),
    );
  }

  Widget _buildChild(Map<String, dynamic> appointment) {
    String _logisticsCode = BaseUtils.getKeyData(appointment, 'logisticsCode', defaultValue: '');
    String _appointmentArea = BaseUtils.getKeyData(appointment, 'appointmentArea');
    DateTime _appointmentTime = BaseUtils.getKeyData(appointment, 'appointmentTime', defaultValue: null);

    if (appointment.length == 0 || _appointmentArea.isEmpty || _appointmentTime.toString().isEmpty) {
      return Text(widget.request.notes ?? '', style: Theme.of(context).inputDecorationTheme.hintStyle);
    }
    String _showText = "${_appointmentTime.year}-${BaseTransformUtil.format(_appointmentTime.month)}-${BaseTransformUtil.format(_appointmentTime.day)} $_appointmentArea";
    if (_logisticsCode == 'SFSDT' || _logisticsCode == "SFTC") if (_appointmentArea.contains('~')) _showText = "${_appointmentTime.year}-${BaseTransformUtil.format(_appointmentTime.month)}-${BaseTransformUtil.format(_appointmentTime.day)} ${_appointmentArea.split('~')[0]}";
    return Text(_showText);
  }

  void _openSelectAppointmentTime(Map<String, dynamic> appointment) {
    String _appointmentArea = BaseUtils.getKeyData(appointment, 'appointmentArea');
    String _logisticsCode = BaseUtils.getKeyData(appointment, 'logisticsCode', defaultValue: '');
    DateTime _appointmentTime = BaseUtils.getKeyData(appointment, 'appointmentTime', defaultValue: null);
    String _hit = '请选择';
    if (_logisticsCode == 'SFSDT') _hit = '请选择开始时间(提前一小时预约)';
    if (_logisticsCode == 'DADA') _hit = '请选择(提前5分钟预约)';
    BaseComponentUtils.openSelectAppointmentTime(context, hit: _hit, appointmentArea: _appointmentArea, appointmentTime: _appointmentTime, logisticsCode: _logisticsCode, onChange: (DateTime time, String area) => _selectAppointmentTimeOnChange(time, area, _logisticsCode));
  }

  void _selectAppointmentTimeOnChange(DateTime time, String area, String logisticsCode) {
    DateTime _dateNow = DateTime.now();
    bool isToday = time.year == _dateNow.year && time.month == _dateNow.month && time.day == _dateNow.day;
    if ((logisticsCode == 'SFSDT' || logisticsCode == "SFTC") && area.isNotEmpty && area.contains(":") && isToday) {
      // 顺丰需要提前1小时
      String _selectStartTime = area.split(":")[0];
      String _selectEndTime = area.split(":")[1];
      int _start = int.tryParse(_selectStartTime);
      int end = int.tryParse(_selectEndTime);
      DateTime _startTime = DateTime(_dateNow.year, _dateNow.month, _dateNow.day, _start, end, 0);
      Duration _difference = _startTime.difference(_dateNow);
      // 1小时 毫秒值
      int _minutes = 60 * 60 * 1000;
      if (_difference.inMilliseconds < _minutes && logisticsCode == "SFSDT") {
        BaseUtils.showToast("顺丰快递请提前1小时预约");
        return;
      }
      if (_difference.inMilliseconds < 0 && logisticsCode == "SFTC") {
        BaseUtils.showToast("顺丰同城快递请提前预约");
        return;
      }
    }
    if (logisticsCode == "DADA" && area.isNotEmpty && area.contains('~') && isToday) {
      String _selectStartTime = area.split(':')[0];
      int _start = int.tryParse(_selectStartTime);
      DateTime _startTime = DateTime(_dateNow.year, _dateNow.month, _dateNow.day, _start, 0, 0);
      Duration _difference = _startTime.difference(_dateNow);
      // 5分钟毫秒数 5 * 60 * 1000
      int _minutes = 5 * 60 * 1000;
      if (_difference.inMilliseconds < _minutes) {
        // 达达需要提前5分钟
        BaseUtils.showToast("达达快送请提前5分钟预约");
        return;
      }
    }
    _baseRequestFormProvider.setDataSource(widget.request, {"appointmentArea": area, 'appointmentTime': time, 'logisticsCode': logisticsCode});
    if (widget.requestSaveData != null) {
      widget.requestSaveData(widget.request, _baseRequestFormProvider.getMapDataSource(widget.request));
    }
    setState(() {});
  }
}
